package com.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 创建队列、交换机、路由及其绑定
 * 
 * @author zincredible
 * @date 2019/06/10 13:58:42
 */
@Configuration
public class RabbitMQConfig {

	@Autowired
	private Environment env;

	/**
	 * 创建交换机
	 * 
	 * @return
	 */
	@Bean
	public DirectExchange basicExchange() {
		return new DirectExchange(env.getProperty("basic.info.mq.exchange.name"), true, false);
	}

	/**
	 * 创建队列
	 * 
	 * @return
	 */
	@Bean(name = "basicQueue")
	public Queue basicQueue() {
		return new Queue(env.getProperty("basic.info.mq.queue.name"), true);
	}

	/**
	 * 将队列和交换机进行绑定
	 * 
	 * @return
	 */
	@Bean
	public Binding basicBinding(Queue basicQueue, DirectExchange basicExchange) {
		return BindingBuilder.bind(basicQueue).to(basicExchange)
				.with(env.getProperty("basic.info.mq.routing.key.name"));
	}

}
